<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Category;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CategoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Category(), function (Grid $grid) {
            $grid->model()->orderBy('sort');
            $grid->column('name')->tree(true);
            $grid->column('sub_name');
            $grid->column('uri')->link(function ($value) {
                if ($value){
                    return url($value);
                }
                return '';
            });
            $grid->column('image')->image('',50,50);
            $grid->column('describe');
            $grid->column('status')->switch();
            $grid->column('sort')->orderable();
            $grid->column('created_at');
            $grid->quickSearch(['name']);
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
        });


    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Category(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('sub_name');
            $show->field('image');
            $show->field('describe');
            $show->field('status');
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Category(), function (Form $form) {
            $form->select('parent_id','上级')->options(
                \App\Models\Category::where('parent_id',0)->pluck('name','id')->prepend('顶级',0)
            )->required()->default(0);
            $form->text('name')->required();
            $form->text('sub_name');
            $form->text('uri');
            $form->image('image')->autoUpload()->uniqueName()->move('category');
            $form->textarea('describe');
            $form->switch('status')->default(1);
            $form->hidden('sort')->default(0);
        });
    }
}
